package com.bjtu.laorenyouhao.yuangong.service.impl;

import com.bjtu.laorenyouhao.yuangong.entity.YuangongInfoEntity;
import com.bjtu.laorenyouhao.yuangong.fegin.LaorenFeignService;
import com.bjtu.laorenyouhao.yuangong.service.YuangongInfoService;
import com.bjtu.laorenyouhao.yuangong.utils.RenyuanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjtu.common.utils.PageUtils;
import com.bjtu.common.utils.Query;

import com.bjtu.laorenyouhao.yuangong.dao.LaorenYuangongDao;
import com.bjtu.laorenyouhao.yuangong.entity.LaorenYuangongEntity;
import com.bjtu.laorenyouhao.yuangong.service.LaorenYuangongService;
import org.springframework.util.StringUtils;


@Service("laorenYuangongService")
public class LaorenYuangongServiceImpl extends ServiceImpl<LaorenYuangongDao, LaorenYuangongEntity> implements LaorenYuangongService {

    @Autowired
    private LaorenFeignService laorenFeignService;

    @Autowired
    private YuangongInfoService yuangongInfoService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        String key = (String) params.get("key");
        QueryWrapper<LaorenYuangongEntity> queryWrapper = new QueryWrapper<LaorenYuangongEntity>();
        if (StringUtils.hasLength(key)){
            queryWrapper.eq("id",key)
                    .or().like("laoren_id", RenyuanUtils.getLaorenId(key,laorenFeignService))
                    .or().eq("yuangong_id",
                    yuangongInfoService.getOne(new QueryWrapper<YuangongInfoEntity>().eq("name",key)).getId());
        }

        IPage<LaorenYuangongEntity> page = this.page(
                new Query<LaorenYuangongEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

}